package com.heima.wemedia.scan.job;

import com.heima.model.wemedia.entity.WmNews;
import com.heima.wemedia.dao.WmNewsDao;
import com.heima.wemedia.scan.service.WmNewsAutoScanService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Component
@Slf4j
public class NewsAutoPublishJob {

    @Resource
    private WmNewsDao wmNewsDao;

    @Resource
    private WmNewsAutoScanService wmNewsAutoScanService;

    @XxlJob("newsAutoPubish")
    public void newsAutoPublish() {
        log.info("文章自动扫描发布任务执行--------------");
        //1. 查询数据库中状态为(4,8)   并且发布时间 <= 当前时间的数据
        List<WmNews> wmNewsList = wmNewsDao.queryPublishNews();
        //2. 调用feign接口发布文章
        //for (WmNews wmNews : wmNewsList) {
        //    wmNewsAutoScanService.saveApArticle(wmNews);
        //}
        wmNewsList.stream().forEach(wmNews -> wmNewsAutoScanService.saveArticle(wmNews));

        log.info("文章自动扫描发布任务执行接收 , 文章id:{}",wmNewsList.stream().map(wmNews -> wmNews.getId()).collect(Collectors.toList()));
    }
}